package ccnt.pvc.scudwaremobile.storage;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import ccnt.pvc.scudwaremobile.dao.ServiceModel;
import ccnt.pvc.scudwaremobile.storage.ServiceContract.ApkEntry;
import ccnt.pvc.scudwaremobile.storage.ServiceContract.ServiceEntry;

public class ServiceDbManager {

	private final String[] service_projection = {
			ServiceEntry.COLUMN_ID,
			ServiceEntry.COLUMN_CLASS_NAME,
			ServiceEntry.COLUMN_PACKAGE_NAME,
			ServiceEntry.COLUMN_DEVICE_NAME,
			ServiceEntry.COLUMN_CATEGORY,
			ServiceEntry.COLUMN_SENSOR_NAME,
			ServiceEntry.COLUMN_DATA_TYPE,
			ServiceEntry.COLUMN_MEASURE,
			ServiceEntry.COLUMN_LEVEL,
			ServiceEntry.COLUMN_PARAMS,
			ServiceEntry.COLUMN_DESCRIPTION,
			ServiceEntry.COLUMN_GMTCREATED,
			ServiceEntry.COLUMN_GMTMODIFIED,
			ServiceEntry.COLUMN_IS_DELETED
		};
	
	private Context context;
	private ServiceDbHelper serviceDbHelper;
	
	public ServiceDbManager(Context context) {
		this.context = context;
	}
	
	public ServiceDbHelper getDbHelper() {
		if(serviceDbHelper == null) {
			serviceDbHelper = new ServiceDbHelper(context);
		}
		return serviceDbHelper;
	}
	
	private ContentValues getContentValues(ServiceModel serviceModel) {
		ContentValues values = new ContentValues();
		values.put(ServiceEntry.COLUMN_CLASS_NAME, serviceModel.getClassName());
		values.put(ServiceEntry.COLUMN_PACKAGE_NAME, serviceModel.getPackageName());
		values.put(ServiceEntry.COLUMN_DEVICE_NAME, serviceModel.getDeviceName());
		values.put(ServiceEntry.COLUMN_CATEGORY, serviceModel.getCategory());
		values.put(ServiceEntry.COLUMN_SENSOR_NAME, serviceModel.getSensorName());
		values.put(ServiceEntry.COLUMN_DATA_TYPE, serviceModel.getDataType());
		values.put(ServiceEntry.COLUMN_MEASURE, serviceModel.getMeasure());
		values.put(ServiceEntry.COLUMN_LEVEL, serviceModel.getLevel());
		values.put(ServiceEntry.COLUMN_PARAMS, serviceModel.getParams());
		values.put(ServiceEntry.COLUMN_DESCRIPTION, serviceModel.getDescription());
		values.put(ServiceEntry.COLUMN_GMTCREATED, serviceModel.getGmtCreated().toString());
		values.put(ServiceEntry.COLUMN_GMTMODIFIED, serviceModel.getGmtModified().toString());
		values.put(ServiceEntry.COLUMN_IS_DELETED, 0);
		return values;
	}
	
	private ServiceModel fillServiceModel(Cursor cursor) {
		ServiceModel service = new ServiceModel();
		service.setClassName(cursor.getString(cursor.getColumnIndex(ServiceEntry.COLUMN_CLASS_NAME)));
		service.setPackageName(cursor.getString(cursor.getColumnIndex(ServiceEntry.COLUMN_PACKAGE_NAME)));
		service.setDeviceName(cursor.getString(cursor.getColumnIndex(ServiceEntry.COLUMN_DEVICE_NAME)));
		service.setCategory(cursor.getInt(cursor.getColumnIndex(ServiceEntry.COLUMN_CATEGORY)));
		service.setSensorName(cursor.getString(cursor.getColumnIndex(ServiceEntry.COLUMN_SENSOR_NAME)));
		service.setDataType(cursor.getString(cursor.getColumnIndex(ServiceEntry.COLUMN_DATA_TYPE)));
		service.setMeasure(cursor.getString(cursor.getColumnIndex(ServiceEntry.COLUMN_MEASURE)));
		service.setLevel(cursor.getInt(cursor.getColumnIndex(ServiceEntry.COLUMN_LEVEL)));
		service.setParams(cursor.getString(cursor.getColumnIndex(ServiceEntry.COLUMN_PARAMS)));
		service.setDescription(cursor.getString(cursor.getColumnIndex(ServiceEntry.COLUMN_DESCRIPTION)));
		return service;
	}
	
	// service_all表操作
	
	/**
	 * 向service_all表插入一条serviceModel
	 * @param serviceModel service实例
	 * @return id
	 */
	public long insertServiceToAll(ServiceModel serviceModel) {
		SQLiteDatabase db = getDbHelper().getWritableDatabase();
		long rowId = db.insert(ServiceEntry.TABLE_NAME_ALL, null, getContentValues(serviceModel));
		db.close();
		return rowId;
	}
	
	/**
	 * 向service_all表插入多条serviceModel
	 * @param serviceModels service列表
	 * @return 插入条数
	 */
	public int insertServicesToAll(List<ServiceModel> serviceModels) {
		SQLiteDatabase db = getDbHelper().getWritableDatabase();
		for(ServiceModel serviceModel : serviceModels) {
			db.insert(ServiceEntry.TABLE_NAME_ALL, null, getContentValues(serviceModel));
		}
		db.close();
		return serviceModels.size();
	}
	
	/**
	 * 从service_all表获得所有service
	 * @return service列表
	 */
	public List<ServiceModel> getAllServices() {
		List<ServiceModel> services = new ArrayList<ServiceModel>();
		SQLiteDatabase db = getDbHelper().getReadableDatabase();
		Cursor cursor = db.query(ServiceEntry.TABLE_NAME_ALL, service_projection, null,
				null, null, null, null);
		while(cursor.moveToNext()) {
			services.add(fillServiceModel(cursor));
		}
		db.close();
		return services;
	}
	
	/**
	 * 从service_all表获得service条数
	 * @return service条数
	 */
	public int getAllServiceCount() {
		return getAllServices().size();
	}
	
	/**
	 * 通过包名获得对应的service
	 * @param packageName 包名
	 * @return service列表
	 */
	public List<ServiceModel> getServicesByPackageName(String packageName) {
		List<ServiceModel> services = new ArrayList<ServiceModel>();
		SQLiteDatabase db = getDbHelper().getReadableDatabase();
		Cursor cursor = db.query(ServiceEntry.TABLE_NAME_ALL,
				service_projection, ServiceEntry.COLUMN_PACKAGE_NAME + " = ?",
				new String[] { packageName }, null, null, null);
		while(cursor.moveToNext()) {
			services.add(fillServiceModel(cursor));
		}
		db.close();
		return services;
	}
	
	/**
	 * 通过类名获得服务
	 * @param className 类名
	 * @return 服务实例
	 */
	public ServiceModel getServiceByClassName(String className) {
		ServiceModel serviceModel = null;
		SQLiteDatabase db = getDbHelper().getReadableDatabase();
		Cursor cursor = db.query(ServiceEntry.TABLE_NAME_ALL,
				service_projection, ServiceEntry.COLUMN_CLASS_NAME + " = ?",
				new String[] { className }, null, null, null);
		if(cursor.moveToFirst()) {
			serviceModel = fillServiceModel(cursor);
		}
		db.close();
		return serviceModel;
	}
	
	// service_reg表操作
	
	/**
	 * 向service_reg表插入一条serviceModel
	 * @param serviceModel service实例
	 * @return id
	 */
	public long insertServiceToReg(ServiceModel serviceModel) {
		SQLiteDatabase db = getDbHelper().getWritableDatabase();
		long rowId = db.insert(ServiceEntry.TABLE_NAME_REG, null, getContentValues(serviceModel));
		db.close();
		return rowId;
	}
	
	/**
	 * 向service_reg表插入多条serviceModel
	 * @param serviceModels service列表
	 * @return 插入条数
	 */
	public int insertServicesToReg(List<ServiceModel> serviceModels) {
		SQLiteDatabase db = getDbHelper().getWritableDatabase();
		for(ServiceModel serviceModel : serviceModels) {
			db.insert(ServiceEntry.TABLE_NAME_REG, null, getContentValues(serviceModel));
		}
		db.close();
		return serviceModels.size();
	}
	
	/**
	 * 从service_reg表获得所有service
	 * @return service列表
	 */
	public List<ServiceModel> getAllRegServices() {
		List<ServiceModel> services = new ArrayList<ServiceModel>();
		SQLiteDatabase db = getDbHelper().getReadableDatabase();
		Cursor cursor = db.query(ServiceEntry.TABLE_NAME_REG, service_projection, null,
				null, null, null, null);
		while(cursor.moveToNext()) {
			services.add(fillServiceModel(cursor));
		}
		db.close();
		return services;
	}
	
	/**
	 * 通过类名获得service
	 * @param className 类名
	 * @return service实例
	 */
	public ServiceModel getRegServiceByClassName(String className) {
		ServiceModel serviceModel = null;
		SQLiteDatabase db = getDbHelper().getReadableDatabase();
		Cursor cursor = db.query(ServiceEntry.TABLE_NAME_REG,
				service_projection, ServiceEntry.COLUMN_CLASS_NAME + " = ?",
				new String[] { className }, null, null, null);
		if(cursor.moveToFirst()) {
			serviceModel = fillServiceModel(cursor);
		}
		db.close();
		return serviceModel;
	}
	
	/**
	 * 从service_reg表中删除service
	 * @param packageName 包名
	 * @return 删除的条数
	 */
	public int deleteRegServiceByPackageName(String packageName) {
		SQLiteDatabase db = getDbHelper().getWritableDatabase();
		int count = db.delete(ServiceEntry.TABLE_NAME_REG, ServiceEntry.COLUMN_PACKAGE_NAME
				+ " = ?", new String[] { packageName });
		db.close();
		return count;
	}
	
}
